# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=libedit
pkgname=('libedit' 'libedit-devel')
pkgver=3.1
pkgrel=20170329
pkgdesc="Libedit is an autotool- and libtoolized port of the NetBSD Editline library."
groups=('libraries')
arch=('i686' 'x86_64')
license=('BSD')
url="https://www.thrysoee.dk/editline/"
depends=('msys2-runtime' 'ncurses' 'sh')
makedepends=('ncurses-devel')
options=('staticlibs')
source=(https://www.thrysoee.dk/editline/${pkgname}-${pkgrel}-${pkgver}.tar.gz
        libedit-20130712-1.src.patch
        libedit-3.0-20120311-msys2.patch)
sha256sums=('91f2d90fbd2a048ff6dad7131d9a39e690fd8a8fd982a353f1333dd4017dd4be'
            'e973c43b6576fbafef69f05c3c95ee256bfb972d3622e8c87658e737415cc258'
            'd810791fc6da262ca712b1be34994ff1b6c80bde5fc4b587e22570f022c2a0c6')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgrel}-${pkgver}"

  patch -p2 -i ${srcdir}/libedit-20130712-1.src.patch
  patch -p1 -i ${srcdir}/libedit-3.0-20120311-msys2.patch
  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgrel}-${pkgver}"

  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --enable-widec \
    CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw" \
    LDFLAGS="$LDFLAGS -L/usr/lib/ncursesw"

  make

  make DESTDIR=${srcdir}/dest install
  install -Dm644 COPYING ${srcdir}/dest//usr/share/licenses/libedit/LICENSE
}

package_libedit() {
  groups=('libraries')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  rm "${pkgdir}"/usr/share/man/man3/history.3 # conflicts with readline
  cp "${pkgdir}"/usr/share/man/man3/editline.3 "${pkgdir}"/usr/share/man/man3/el.3
}

package_libedit-devel() {
  pkgdesc="libedit headers and libraries"
  groups=('development')
  depends=("libedit=${pkgver}" 'ncurses-devel')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
